<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>优雅的文本序列动画</title>
    <style>
        :root {
            --primary: #3b82f6;
            --primary-dark: #2563eb;
            --text: #1f2937;
            --bg: #f9fafb;
        }

        body {
            font-family: 'Inter', system-ui, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: var(--bg);
            margin: 0;
            color: var(--text);
            line-height: 1.5;
        }

        .container {
            max-width: 800px;
            padding: 2rem;
        }

        .sequence-text {
            font-size: clamp(1.75rem, 4vw, 2.5rem);
            font-weight: 700;
            line-height: 1.3;
            margin-bottom: 2rem;
        }

        .line {
            display: block;
            overflow: hidden;
            margin-bottom: 0.5em;
            min-height: 1.2em;
        }

        .word {
            display: inline-block;
            position: relative;
            margin-right: 0.25em;
            opacity: 0;
            transform: translateY(0.5em);
        }

        .btn {
            padding: 0.75rem 1.5rem;
            background: linear-gradient(to right, var(--primary), var(--primary-dark));
            color: white;
            border: none;
            border-radius: 0.375rem;
            font-size: 1rem;
            font-weight: 500;
            cursor: pointer;
            transition: transform 0.2s, box-shadow 0.2s;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
        }

        .btn:active {
            transform: translateY(0);
        }
    </style>
</head>
<body>
<div class="container">
    <div id="text" class="sequence-text">
        第一行文本动画效果<br>
        第二行会依次出现<br>
        第三行动画更加流畅<br>
        第四行完成整个序列
    </div>
    <button class="btn" id="replay">重新播放序列</button>
</div>

<!-- 无需手动注册，可直接使用 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.4/gsap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gsap@3.13.0/dist/SplitText.min.js"></script>
<script>
    // 注册插件
    gsap.registerPlugin(SplitText);

    let splitInstance;
    let sequenceTL;

    function initSequenceAnimation() {
        // 清除现有实例和动画
        if (splitInstance) splitInstance.revert();
        if (sequenceTL) sequenceTL.kill();

        // 使用SplitText.create()静态方法
        splitInstance = SplitText.create("#text", {
            type: "lines,words",
            linesClass: "line",
            wordsClass: "word",
            onSplit: (self) => {
                // 创建主时间轴
                sequenceTL = gsap.timeline({
                    defaults: {
                        ease: "power3.out",
                        duration: 0.8
                    }
                });

                // 为每行添加动画
                self.lines.forEach((line, lineIndex) => {
                    const words = line.querySelectorAll(".word");

                    // 每行单词的动画
                    sequenceTL.to(words, {
                        opacity: 1,
                        y: 0,
                        stagger: {
                            each: 0.08,
                            from: "start"
                        },
                        onStart: () => {
                            // 添加行激活类（可用于额外效果）
                            line.classList.add('active');
                        },
                        onComplete: () => {
                            line.classList.remove('active');
                        }
                    }, lineIndex * 0.6);
                });
            }
        });
    }

    // 初始化动画
    document.addEventListener('DOMContentLoaded', initSequenceAnimation);

    // 重新播放动画
    document.getElementById("replay").addEventListener("click", initSequenceAnimation);

    // 键盘快捷键 (R键重新播放)
    document.addEventListener("keydown", (e) => {
        if (e.key.toLowerCase() === "r") {
            initSequenceAnimation();
        }
    });
</script>
</body>
</html>